package cn.buk.tms.entity.misc;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Transient;

/**
 * 航班信息
 * @author yfdai
 *
 */
@Embeddable
public class FlightInfo implements Cloneable {

	private static final Logger logger = LogManager.getLogger(FlightInfo.class);

	/**
	 * departure airport
	 */
	@Column(length = 3, name = "dport")
	private String departureAirport;

	@Column(name = "dport_name", length = 30)
	private String departureAirportName;

	/**
	 * arrival airport
	 */
	@Column(length = 3, name = "aport")
	private String arrivalAirport;

	@Column(name = "aport_name", length = 30)
	private String arrivalAirportName;

	/**
	 * 航班号
	 */
	@Column(length = 8)
	private String flightNo;

	@Column(length = 10, name = "ddate")
	private String departureDate;

	@Column(length = 5, name = "dtime")
	private String departureTime;

	@Column(length = 10, name = "adate")
	private String arrivalDate;

	@Column(length = 5, name = "atime")
	private String arrivalTime;

	@Column(length = 2, name = "dterm")
	private String departureTerminal;

	@Column(length = 2, name = "aterm")
	private String arrivalTerminal;

	/**
	 * 子舱位
	 */
	@Column(length = 2)
	private String subclass;

	/**
	 * 物理舱位
	 */
	@Column(name = "cabin_class", length = 1)
	private String cabinClass;

	@Column(length = 3, name = "aircraft")
	private String craftType;

	private int price;

	private int tax;

	@Column(length = 10)
	private String tktStatus;

	@Column(length = 2, name = "seg_status")
	private String segmentStatus;

	@Transient
	private String routeDetail;

	/**
	 * 用于改签标记
	 * 0 - 改签前的航班
	 * 1 - 改签后的航班
	 */
	@Transient
	private int flightType;

	/**
	 * 航司名的简称
	 */
	@Transient
	private String airlineName;

	/**
	 * 舱位性质
	 */
	@Transient
	private int cabinType;

	/**
	 * 航班信息是否有效
	 *
	 * @return
	 */
	public boolean isValid() {
		if (this.departureAirport == null || this.departureAirport.length() != 3) {
			return false;
		}
		if (this.arrivalAirport == null || this.arrivalAirport.length() != 3) {
			return false;
		}
		if (this.flightNo == null || this.flightNo.length() < 5) {
			return false;
		}
		if (this.subclass == null || this.subclass.length() < 1 || this.subclass.length() > 2) {
			return false;
		}
		return this.departureDate != null && this.departureDate.length() == 10;
	}

	public String getDport() {
		return this.getDepartureAirport();
	}

	public void setDport(String val) {
		this.setDepartureAirport(val);
	}

	public String getAport() {
		return this.getArrivalAirport();
	}

	public void setAport(String val) {
		this.setArrivalAirport(val);
	}

	public String getDdate() {
		return this.getDepartureDate();
	}

	public void setDdate(String val) {
		this.setDepartureDate(val);
	}

	public String getSubClass() {
		return this.getSubclass();
	}

	public String getDtime() {
		return this.getDepartureTime();
	}

	public void setDtime(String val) {
		this.setDepartureTime(val);
	}

	public String getAtime() {
		return this.getArrivalTime();
	}

	public void setAtime(String val) {
		this.setArrivalTime(val);
	}

	public String getDterm() {
		return this.getDepartureTerminal();
	}

	public void setDterm(String val) {
		this.setDepartureTerminal(val);
	}

	public String getAterm() {
		return this.getArrivalTerminal();
	}

	public void setAterm(String val) {
		this.setArrivalTerminal(val);
	}

	/**
	 * @return the departureAirport
	 */
	public String getDepartureAirport() {
		return departureAirport == null ? "" : departureAirport.trim().toUpperCase();
	}

	/**
	 * @param departureAirport the departureAirport to set
	 */
	public void setDepartureAirport(String departureAirport) {
		if (departureAirport != null) {
			departureAirport = departureAirport.trim().toUpperCase();
		}
		this.departureAirport = departureAirport;
	}

	/**
	 * @return the arrivalAirport
	 */
	public String getArrivalAirport() {
		return arrivalAirport == null ? "" : arrivalAirport.trim().toUpperCase();
	}

	/**
	 * @param arrivalAirport the arrivalAirport to set
	 */
	public void setArrivalAirport(String arrivalAirport) {
		if (arrivalAirport != null) {
			arrivalAirport = arrivalAirport.trim().toUpperCase();
		}
		this.arrivalAirport = arrivalAirport;
	}

	/**
	 * @return the flightNo
	 */
	public String getFlightNo() {
		return flightNo == null ? "" : flightNo.trim().toUpperCase();
	}

	/**
	 * @param flightNo the flightNo to set
	 */
	public void setFlightNo(String flightNo) {
		if (flightNo != null) {
			flightNo = flightNo.trim().toUpperCase();
		}
		this.flightNo = flightNo;
	}

	/**
	 * @return the departureDate
	 */
	public String getDepartureDate() {
		return departureDate == null ? "" : departureDate.trim();
	}

	/**
	 * @param departureDate the departureDate to set
	 */
	public void setDepartureDate(String departureDate) {
		this.departureDate = departureDate;
	}

	/**
	 * @return the departureTime
	 */
	public String getDepartureTime() {
		return departureTime == null ? "" : departureTime.trim();
	}

	/**
	 * @param departureTime the departureTime to set
	 */
	public void setDepartureTime(String departureTime) {
		this.departureTime = departureTime;
	}

	/**
	 * @return the arrivalDate
	 */
	public String getArrivalDate() {
		return arrivalDate;
	}

	/**
	 * @param arrivalDate the arrivalDate to set
	 */
	public void setArrivalDate(String arrivalDate) {
		this.arrivalDate = arrivalDate;
	}

	/**
	 * @return the arrivalTime
	 */
	public String getArrivalTime() {
		return arrivalTime == null ? "" : arrivalTime.trim();
	}

	/**
	 * @param arrivalTime the arrivalTime to set
	 */
	public void setArrivalTime(String arrivalTime) {
		this.arrivalTime = arrivalTime;
	}

	/**
	 * @return the departureTerminal
	 */
	public String getDepartureTerminal() {
		return departureTerminal == null ? "" : departureTerminal.trim();
	}

	/**
	 * @param departureTerminal the departureTerminal to set
	 */
	public void setDepartureTerminal(String departureTerminal) {
		this.departureTerminal = departureTerminal;
	}

	/**
	 * @return the arrivalTerminal
	 */
	public String getArrivalTerminal() {
		return arrivalTerminal == null ? "" : arrivalTerminal.trim().toUpperCase();
	}

	/**
	 * @param arrivalTerminal the arrivalTerminal to set
	 */
	public void setArrivalTerminal(String arrivalTerminal) {
		this.arrivalTerminal = arrivalTerminal;
	}

	/**
	 * @return the subclass
	 */
	public String getSubclass() {
		return subclass == null ? "" : subclass.trim();
	}

	/**
	 * @param subclass the subclass to set
	 */
	public void setSubclass(String subclass) {
		if (subclass != null) {
			subclass = subclass.trim().toUpperCase();
		}
		this.subclass = subclass;
	}

	/**
	 * @return the craftType
	 */
	public String getCraftType() {
		return craftType == null ? "" : craftType.trim().toUpperCase();
	}

	/**
	 * @param craftType the craftType to set
	 */
	public void setCraftType(String craftType) {
		this.craftType = craftType;
	}

	/**
	 * @return the price
	 */
	public int getPrice() {
		return price;
	}

	/**
	 * @param price the price to set
	 */
	public void setPrice(int price) {
		this.price = price;
	}

	/**
	 * @return the tax
	 */
	public int getTax() {
		return tax;
	}

	/**
	 * @param tax the tax to set
	 */
	public void setTax(int tax) {
		this.tax = tax;
	}

	/**
	 * @return the tktStatus
	 */
	public String getTktStatus() {
		return tktStatus;
	}

	/**
	 * @param tktStatus the tktStatus to set
	 */
	public void setTktStatus(String tktStatus) {
		this.tktStatus = tktStatus;
	}

	/**
	 * @return the segmentStatus
	 */
	public String getSegmentStatus() {
		return segmentStatus;
	}

	/**
	 * @param segmentStatus the segmentStatus to set
	 */
	public void setSegmentStatus(String segmentStatus) {
		this.segmentStatus = segmentStatus;
	}

	public String getRouteDetail() {
		if (routeDetail == null) {
			routeDetail = departureAirport + arrivalAirport + "/" + flightNo + "/" + subclass + "/" + departureDate
							+ "/" + departureTime + "/" + arrivalTime + "/" + getDepartureTerminal() + "/" + getArrivalTerminal();
		}
		return routeDetail;
	}

	public void processRouteDetail(String routeDetail) {
		String[] items = routeDetail.split("/");
		try {
			this.setDepartureAirport(items[0].substring(0, 3));
			this.setArrivalAirport(items[0].substring(3));

			this.setFlightNo(items[1]);
			this.setSubclass(items[2]);
			this.setDepartureDate(items[3]);

			if (items.length >= 6) {
				this.setDepartureTime(items[4]);
				this.setArrivalTime(items[5]);
			}

			if (items.length >= 7) {
				this.setDepartureTerminal(items[6]);
			}

			if (items.length >= 8) {
				this.setArrivalTerminal(items[7]);
			}

			if (items.length >= 9) {
				this.setFlightType(Integer.parseInt(items[8]));
			}
		} catch (Exception ex) {
			logger.error(ex.getMessage() + ". :" + routeDetail);
		}

	}

	public void setRouteDetail(String routeDetail) {
		this.routeDetail = routeDetail;
	}

	public String getDepartureAirportName() {
		return departureAirportName;
	}

	public void setDepartureAirportName(String departureAirportName) {
		this.departureAirportName = departureAirportName;
	}

	public String getArrivalAirportName() {
		return arrivalAirportName;
	}

	public void setArrivalAirportName(String arrivalAirportName) {
		this.arrivalAirportName = arrivalAirportName;
	}


	public int getFlightType() {
		return flightType;
	}

	public void setFlightType(int flightType) {
		this.flightType = flightType;
	}

	public String getAirlineName() {
		return airlineName == null ? "" : airlineName.trim();
	}

	public void setAirlineName(String airlineName) {
		this.airlineName = airlineName;
	}

	public int getCabinType() {
		return cabinType;
	}

	public void setCabinType(int cabinType) {
		this.cabinType = cabinType;
	}

	public String getCabinClass() {
		return cabinClass;
	}

	public void setCabinClass(String cabinClass) {
		this.cabinClass = cabinClass;
	}
}
